from flask_shop.wxuser import wxuser,wxuser_api
from flask_shop import models,db
from flask import request,current_app
from flask_restful import Resource,reqparse
import re
from flask_shop.utils.md5 import md5_file
from flask_shop.utils.message import to_dict_msg

class WxUser(Resource):
	def get(self):
		try:
			wxid=request.args.get('wxid')
			wxuser=models.WxUser.query.filter_by(wxid=wxid).first()
			if wxuser:
				usr=models.User.query.get(wxuser.user_id)
				return to_dict_msg(200,data=usr.to_dict(),msg="获取成功")
			else:
				return to_dict_msg(10035)
		except Exception as e:
			print(e)
			return to_dict_msg(20000)
	def post(self):
		try:
			user_id=request.form.get('user_id')
			nick_name=request.form.get('nick_name')
			avatar_url=request.form.get('avatar_url')
			wxid=request.form.get('wxid')
			usr=models.User.query.get(user_id)
			if usr:				
				wxuser=models.WxUser(nick_name=nick_name,avatar_url=avatar_url,user_id=user_id,wxid=wxid)
				db.session.add(wxuser)
				db.session.commit()
				return to_dict_msg(200,msg="绑定成功")
			else:
				return to_dict_msg(10011)
		except Exception as e:
			print(e)
			return to_dict_msg(20000)	
	def delete(self):
		try:
			wxid=request.form.get("wxid")
			uid=int(request.form.get("uid"))
			wxuser=models.WxUser.query.filter_by(wxid=wxid,user_id=uid).first()
			if wxuser:
				db.session.delete(wxuser)
				db.session.commit()
				return to_dict_msg(200,msg="解绑成功")
			else:
				return to_dict_msg(10035)
		except Exception as e:
			print(e)
			return to_dict_msg(20000)

@wxuser.route("/setDeviceName",methods=["GET"])
def setDeviceName():
	try:
		wxid=request.args.get("wxid")
		deviceName=request.args.get("deviceName")
		wxuser=models.WxUser.query.filter_by(wxid=wxid).first()
		if wxuser:
			wxuser.deviceName=deviceName
			db.session.commit()
			return to_dict_msg(200,msg='设置设备成功')
		else:
			return to_dict_msg(10037)
	except Exception as e:
		print(e)
		current_app.logger.error(e)
		return to_dict_msg(20000)
@wxuser.route("/setDeviceStatus",methods=['GET'])
def setDeviceStatus():
	try:
		status=int(request.args.get("status"))
		wxid=request.args.get("wxid")
		wxuser=models.WxUser.query.filter_by(wxid=wxid).first()
		if wxuser:
			wxuser.deviceStatus=status
			db.session.commit()	
			return to_dict_msg(200,msg='设置设备状态成功')
		else:
			return to_dict_msg(10037)
	except Exception as e:
		print(e)
		current_app.logger.error(e)
		return to_dict_msg(20000)
@wxuser.route("/getWxuser",methods=['GET'])
def getWxuser():
	try:
		uid=request.args.get("uid")
		wxuser=models.WxUser.query.filter_by(user_id=uid).first()
		if wxuser:
			return to_dict_msg(200,data=wxuser.to_dict(),msg="获取微信用户成功")
		else:
			return to_dict_msg(10037)
	except Exception as e:
		print(e)
		current_app.logger.error(e)
		return to_dict_msg(20000)
# 设置传感器数据
@wxuser.route("/setDistantData",methods=['GET'])
def setDistantData():
	try:
		distantData=float(request.args.get("distantData"))
		wxid=request.args.get("wxid")
		sensorData=models.DistantSensor(wxid=wxid,distantData=distantData)
		wxuser=models.WxUser.query.filter_by(id=wxid).first()
		if wxuser:
			wxuser.did.append(sensorData)
			db.session.commit()
			return to_dict_msg(200,msg='设置数据成功')
		else:
			return to_dict_msg(10037)
	except Exception as e:	
		print(e)
		current_app.logger.error(e)
		return to_dict_msg(20000)
# 获取传感器数据
@wxuser.route("/getDistantData",methods=['GET'])
def getDistantData():
	try:
		types=request.args.get("types")
		wxid=request.args.get("wxid")
		wxuser=models.WxUser.query.filter_by(id=wxid).first()
		if wxuser:
			if types=='latest':
				distantData=models.DistantSensor.query.filter_by(wxid=wxid).order_by(models.DistantSensor.create_time.desc()).first()
				return to_dict_msg(200,data=distantData.to_dict(),msg="获取数据成功")
			elif types=='history':
				distantData=models.DistantSensor.query.filter_by(wxid=wxid).order_by(models.DistantSensor.create_time.desc()).limit(50).all()
				dlist=[d.to_dict() for d in distantData]
				return to_dict_msg(200,data=dlist,msg="获取数据成功")
		else:
			return to_dict_msg(10037)
	except Exception as e:
		print(e)
		current_app.logger.error(e)
		return to_dict_msg(20000)
# 清空数据
@wxuser.route("/clearSensorData",methods=["DELETE"])
def clearSensorData():
	try:
		wxid=request.args.get("wxid")
		wxuser=models.WxUser.query.get(wxid)
		if wxuser:
			models.DistantSensor.query.filter_by(wxid=wxid).delete()
			db.session.commit()
			return to_dict_msg(200,msg="清空数据成功")
		else:
			return to_dict_msg(10037)
	except Exception as e:
		print(e)
		db.session.rollback()
		current_app.logger.error(e)
		return to_dict_msg(20000)
wxuser_api.add_resource(WxUser,"/wxuser")
